package Five;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
 * description:接收发送思路参考客户端。。。
 * @author 长风无言
 */
public class NewServer {

    private ServerSocket serverSocket = null;
    private List<Socket> socketList = new ArrayList<>();
    private Socket s =null;

    public static void main(String[] args) {
        NewServer ns = new NewServer();

        try {
            ns.serverSocket = new ServerSocket(8888);
            while (true) {
                ns.s = ns.serverSocket.accept();
                ns.socketList.add(ns.s);
                System.out.println("客户端"+ns.s.getInetAddress()+"连接成功");
                ns.Work(ns.s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != ns.serverSocket){
                try {
                    ns.serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void Work(Socket s){
        new Thread(()->{
            DataOutputStream out = null;
            DataInputStream in = null;

            try {
                in = new DataInputStream(s.getInputStream());
                out = new DataOutputStream(s.getOutputStream());
                while (true) {
                    String str = in.readUTF();
                    System.out.println("接收完毕！");
                    if ("file".equals(str)) {
                        String str1 = in.readUTF();
                        String name = str1.split(";")[0];
                        int length = Integer.parseInt(str1.split(";")[1]);
                        System.out.println("开始接收文件" + name + "文件长度为：" + length);
                        File f = new File("d:/test2/" + name);
                        FileOutputStream fout = new FileOutputStream(f);
                        byte[] arry = new byte[8 * 1024];
                        int res = 0;
                        System.out.println("开始写入文件！");
                        while (true) {
                            res = in.read(arry, 0, arry.length);
                            fout.write(arry, 0, res);
                            fout.flush();
                            if (f.length() == length) {
                                System.out.println("文件接收完毕！");
                                break;
                            }
                        }
                        System.out.println("开始转发文件！");
                        for (Socket ss : socketList) {
                            out = new DataOutputStream(ss.getOutputStream());
                            out.writeUTF("file" + ";" + name + ";" + (String.valueOf(f.length())));
                            f = new File("d:/test2/" + name);
                            FileInputStream fin = new FileInputStream(f);
                            byte[] arry1 = new byte[8 * 1024];
                            res = 0;
                            while ((res = fin.read(arry1)) != -1) {
                                out.write(arry1, 0, res);
                                out.flush();
                            }
                            System.out.println("转发完毕！");
                        }
                    } else {
                        System.out.println("客户端" + s.getInetAddress() + "发来的消息是：" + str);
                        for (Socket ss : socketList) {
                            out = new DataOutputStream(ss.getOutputStream());
                            out.writeUTF(ss.getInetAddress() + " : " + str);
                        }
                    }
                    System.out.println("服务器回复消息成功！");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(null!=in){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(null!=out){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        ).start();
    }

}
